package com.example.tired1.zyfypt_14120200131_syn.model;

import com.example.tired1.zyfypt_14120200131_syn.bean.Constant;
import com.example.tired1.zyfypt_14120200131_syn.iface.RegisterListener;
import com.example.tired1.zyfypt_14120200131_syn.service.RegisterService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by tired1 on 2017/5/9.
 */

public class RegisterMode {
    Retrofit retrofit;

    public  RegisterMode(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASEURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

    }
    public void register(String username, String password, String tel, final RegisterListener listener){
        RegisterService service=retrofit.create(RegisterService.class);
        Call<String> call=service.getregister(username,password,tel);//声明请求
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.body()!=null && response.isSuccessful()){
                    listener.onResponse(response.body());
                    //使用activity中实现的接口对象，将异步返回的结果，返回到activity中处理
                }
                else {
                    System.out.println("==>>"+"response fall!");
                    listener.onfail("onResponse fail");//使用了Java的回调机制

                }

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                System.out.println("==>>"+"response onFail!");
                listener.onfail(t.toString());


            }
        });
    }


}
